package com.soft.link.controller;

import com.alibaba.fastjson.JSON;
import com.soft.link.impl.RedisService;
import com.soft.link.model.TsUser;
import com.soft.link.service.TsUserService;
import com.soft.link.utils.AppMsgEnum;
import com.soft.link.utils.UniqueUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * company 重庆庆云石油工程技术有限责任公司
 * FileName AppLoginController
 * Package com.soft.link.controller
 * Description
 * author Administrator
 * create 2018-03-05 20:03
 * version V1.0
 */
@Controller
@RequestMapping(value = "/app/portal")
public class AppLoginController extends BaseController {
    @Autowired(required = false)
    RedisService redisService;
    @Autowired(required = false)
    private TsUserService tsUserService;

    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/doLogin", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map doLogin(String loginSUser, String passwordSUser) {
        Map<String, Object> resCode = new HashMap<>();
        try {
            TsUser tsUser = tsUserService.findTsUser(loginSUser, passwordSUser);
            if (tsUser == null) {
                resCode.put("status", false);
                resCode.put("msgCode", AppMsgEnum.APP_Login_No_TsUser.getCode());
                resCode.put("msgContent", AppMsgEnum.APP_Login_No_TsUser.getDesc());
            } else {
                String token = UniqueUtil.uuid(); // 使用java原生的进行生产token
                if (tsUser.getIslock() == 0) {
                    redisService.set(token, JSON.toJSONString(tsUser));
                    resCode.put("status", true);
                    resCode.put("msgCode", AppMsgEnum.App_Login_Yes_Success.getCode());
                    resCode.put("msgContent", AppMsgEnum.App_Login_Yes_Success.getDesc());
                    resCode.put("data", JSON.toJSONString(tsUser));
                    resCode.put("token", token);
                } else {
                    resCode.put("status", false);
                    resCode.put("msgCode", AppMsgEnum.App_Login_No_IsLock.getCode());
                    resCode.put("msgContent", AppMsgEnum.App_Login_No_IsLock.getDesc());
                }
                logger.info("-------------->redis:" + redisService.get(token));
            }
        } catch (Exception e) {
            resCode.put("status", false);
            resCode.put("msgCode", AppMsgEnum.APP_Login_Exception_Service.getCode());
            resCode.put("msgContent", AppMsgEnum.APP_Login_Exception_Service.getDesc());
        }
        return resCode;
    }

}
